package com.easepal802s.project.observer;

import com.clj.fastble.data.BleDevice;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FHB on 2018/4/18.
 */
public class BleDataObserver {

    private static BleDataObserver mInst = null;
    private static Object mLock = new Object();

    private List<OnBleListerner> mOnBleListerner;

    private BleDataObserver(){
        mOnBleListerner = new ArrayList<OnBleListerner>();
    }

    public static BleDataObserver getInst(){
        if(mInst == null){
            synchronized(mLock){
                if(mInst == null){
                    mInst = new BleDataObserver();
                }
            }
        }
        return mInst;
    }

    public void  registerBleDataObserver(OnBleListerner action){
        if(!mOnBleListerner.contains(action)){
            mOnBleListerner.add(action);
        }
    }

    public void removeBleDataObserver(OnBleListerner action){
        mOnBleListerner.remove(action);
    }

    /**
     * 连接状态变化 用于显示连接中 连接成功 连接失败
     * @param state
     */
    public void noticeConnectStateChange(int state){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onConnectState(state);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 发现目标设备 用于蓝牙搜索的显示
     * @param bleDevice
     */
    public void noticeDeviceFind(BleDevice bleDevice){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onTargetDeviceFind(bleDevice);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 开始扫描 用于提示开始扫描
     * @param
     */
    public void noticeStarScan(){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onStarScan();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 没有设备发现 用于提示没有目标设备发现
     * @param
     */
    public void noticeNoDeviceFind(){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onNoDeviceFind();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 收到数据
     * @param
     */
    public void noticeGetData(String data){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onGetData(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void noticePowerOff(){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onPowerOff();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void noticeDataChange(String bage,int auto,int skill,int air,String name,String commond){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onDataChange(bage,auto,skill,air,name,commond);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void noticeShoulderAdjust(int data){
        for (int i = 0; i < mOnBleListerner.size(); i++) {
            try {
                mOnBleListerner.get(i).onShoulderAdjust(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface OnBleListerner {
        //连接状态
        void onConnectState(int state);
        //找到目标设备
        void onTargetDeviceFind(BleDevice bleDevice);
        void onStarScan();
        void onNoDeviceFind();
        void onGetData(String data);
        void onPowerOff();
        void onDataChange(String bage,int auto,int skill,int air,String name,String commond);
        void onShoulderAdjust(int data);

    }


}
